package com.lk.org.controller;

import com.lk.basic.util.AjaxResult;
import com.lk.basic.util.PageList;
import com.lk.org.domain.Employee;
import com.lk.org.query.EmployeeQuery;
import com.lk.org.service.IEmployeeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Currency;
import java.util.List;

@RestController
@RequestMapping("/employee")
@Api(value = "员工的API",description="员工相关的CRUD功能")
public class EmployeeController {

    @Autowired
    private IEmployeeService employeeService;

    @ApiOperation(value ="增改" )
    @PutMapping
    public AjaxResult save(@RequestBody Employee employee){
        try {
            if (employee.getId()!=null){
                employeeService.update(employee);
            }else {
                employeeService.insert(employee);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("ERROR"+e.getMessage());
        }
        return AjaxResult.me();
    }
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除")
    public AjaxResult delById(@ApiParam(required = true) @PathVariable("id") Long id){
        try {
            employeeService.delete(id);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("ERROR"+e.getMessage());
        }
        return AjaxResult.me();
    }

    @ApiParam

    @ApiOperation(value = "查询单个")
    @GetMapping("/{id}")
    public AjaxResult getById(@ApiParam(required = true) @PathVariable("id")Long id){
        try {
            Employee employee = employeeService.selById(id);
            return AjaxResult.me().setResult(employee);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("ERROR"+e.getMessage());
        }
    }
    @ApiOperation(value = "查询所有" )
    @GetMapping
    public AjaxResult getAll(){
        try {
            List<Employee> lists=employeeService.selAll();
            return AjaxResult.me().setResult(lists);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        }
    }
    @ApiOperation(value = "分页")
    @PostMapping
    public AjaxResult pageSize(@RequestBody EmployeeQuery query){
        try {
            PageList<Employee> pageList = employeeService.queryPage(query);
            return AjaxResult.me().setResult(pageList);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("ERROR"+e.getMessage());
        }
    }
}
